Skip to content

Add endpoint for purl lookup - #1359

Merged
TG1999 merged 3 commits into
aboutcode-org:mainfrom
TG1999:lookup_endpoint
Dec 14, 2023
Merged

TG1999 merged 3 commits into
aboutcode-org:mainfrom
TG1999:lookup_endpoint

Conversation

@TG1999

@TG1999 TG1999 commented Dec 6, 2023

Copy link
Copy Markdown
Contributor

fixes : #1357

@johnmhoran

Copy link
Copy Markdown
Member

@TG1999 I tried to test this new PURL lookup endpoint but cannot find the branch lookup_endpoint anywhere in the nexB repo. How do I access that branch? Or is that branch not in the nexB vulnerablecode repo (where I expect it should be)? It almost looks like the branch is in a fork on your own repo, but I don't know why that would be the case -- all of our work should be in the nexB vulnerablecode repo, right?

@johnmhoran

Copy link
Copy Markdown
Member

@TG1999 BTW, I've been doing some initial testing of our API bulk PURL search endpoint and other search endpoints/methods -- using pkg:nginx/nginx@0.8.9?os=windows which we know has two duplicate records with the same PURL -- and have found that:

  • my local .py single-PURL API search returns 3 PURLs: 2 dupes of pkg:nginx/nginx@0.8.9?os=windows plus pkg:nginx/nginx@0.8.9

  • my local .py bulk-PURL API search returns 2 PURLs: 2 dupes of pkg:nginx/nginx@0.8.9?os=windows -- just what we want (except for the duplication) i.e., only the PURL I searched for -- works for a single-PURL search so maybe we don't need a new lookup endpoint?

  • public Package UI single-PURL search returns 3 PURLs: 2 dupes of pkg:nginx/nginx@0.8.9?os=windows plus pkg:nginx/nginx@0.8.9

  • public API UI single-PURL search (GET /api/packages/) returns 3 PURLs: 2 dupes of pkg:nginx/nginx@0.8.9?os=windows plus pkg:nginx/nginx@0.8.9

  • public API UI bulk-PURL search (POST /api/packages/bulk_search/): throws an error

Unable to get this to work.

The required input template differs from the structure that works in my local .py file.

{
  "purl": "string",
  "type": "string",
  "namespace": "string",
  "name": "string",
  "version": "string",
  "qualifiers": {
    "additionalProp1": "string",
    "additionalProp2": "string",
    "additionalProp3": "string"
  },
  "subpath": "string"
}

I tried this without success:

{
  "purl": "pkg:nginx/nginx@0.8.9?os=windows",
  "type": "nginx",
  "namespace": "",
  "name": "nginx",
  "version": "0.8.9",
  "qualifiers": {
    "os": "windows"
  },
  "subpath": ""
}

Response:

{
  "Error": "A non-empty 'purls' list of PURLs is required."
}

I also tried the following structure without success (but this DOES work in my local .py file AND is recommended in our RTD (https://vulnerablecode.readthedocs.io/en/latest/api.html#package-bulk-search)):

{
    "purls": ["pkg:nginx/nginx@0.8.9?os=windows"],
    "purl_only": False,
    "plain_purl": False,
}

Response:

{
  "detail": "JSON parse error - Expecting value: line 3 column 18 (char 70)"
}

@johnmhoran

Copy link
Copy Markdown
Member

Does the public API UI bulk-PURL search (POST /api/packages/bulk_search/) actually work and I just can't follow directions? ;-)

@TG1999

TG1999 commented Dec 12, 2023

Copy link
Copy Markdown
Contributor Author

@johnmhoran

Copy link
Copy Markdown
Member

@TG1999 I don't think you read my comments very closely. I already have the bulk search API working in my Python code -- but I am unable to get the Public API UI bulk search working (here: https://public.vulnerablecode.io/api/docs/), and I explained what the experience was with that. I also do not know where your lookup branch is. All of that is detailed in my comments above, including my successful .py use of this structure:

{
    "purls": ["pkg:nginx/nginx@0.8.9?os=windows"],
    "purl_only": False,
    "plain_purl": False,
}

@TG1999 TG1999 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nits for my consideration

Comment thread vulnerabilities/api.py Outdated
"""
Return the response for exact PackageURL requested for.
"""
purl = request.data.get("purl", []) or None

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
purl = request.data.get("purl", []) or None
purl = request.data.get("purl")

Comment thread vulnerabilities/api.py
@@ -347,6 +347,20 @@ def all(self, request):
vulnerable_purls = [str(package.package_url) for package in vulnerable_packages]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add bulk_lookup

Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
@TG1999
TG1999 requested review from pombredanne and tdruez December 12, 2023 20:32
@TG1999

TG1999 commented Dec 12, 2023

Copy link
Copy Markdown
Contributor Author

Added bulk_lookup endpoint with tests, please review

@johnmhoran

Copy link
Copy Markdown
Member

@TG1999 I've added your repo as a new remote and can now access your lookup_endpoint branch. So far I've focused only on the new lookup endpoint.

Based on the tests, I expected this structure to work with my local instance:

headers = {"Authorization": f"Token {token}"}

request_body = {"purl": "pkg:nginx/nginx@0.8.9?os=windows"}

response = requests.post(
    "http://127.0.0.1:8001/api/packages/lookup",
    data=json.dumps(request_body),
    content_type="application/json",
    headers=headers,
).json()

However, this threw an error:

TypeError: Session.request() got an unexpected keyword argument 'content_type'

Not sure why that works in your tests but not for me. Googling was unhelpful, and the variations I tried in the key 'content_type' did not resolve the error. I finally deleted that line from the requests.post() content and instead modified my headers like this:

headers = {"Authorization": f"Token {token}", "Content-Type": "application/json"}

and the following returned the expected list containing two duplicate pkg:nginx/nginx@0.8.9?os=windows PURLs:

request_body = {"purl": "pkg:nginx/nginx@0.8.9?os=windows"}

response = requests.post(
    "http://127.0.0.1:8001/api/packages/lookup",
    data=json.dumps(request_body),
    headers=headers,
).json()

I'll continue my exploration of the lookup endpoint and then turn to the bulk_lookup endpoint.

@johnmhoran

Copy link
Copy Markdown
Member

@TG1999 Very nice! 👍

@johnmhoran johnmhoran left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!!

@TG1999
TG1999 merged commit 939055a into aboutcode-org:main Dec 14, 2023
@TG1999
TG1999 deleted the lookup_endpoint branch December 14, 2023 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add an endpoint for lookup for exact match in VCIO

2 participants